package com.dm.controller;

import com.dm.constants.Constants;
import com.dm.dto.UserRegisterDto;
import com.dm.pojo.DmUser;
import com.dm.service.DmUserService;
import com.dm.utils.ResponseApi;
import com.dm.utils.Result;
import com.dm.vo.TokenVo;
import com.dm.vo.UserInfoVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author DongJunJun
 * @Date 2022/9/6 11:46
 */
@RestController
public class DmUserController {

    @Resource
    private DmUserService dmUserService;

    /**
     * 获取验证码
     * @param map 手机号/邮箱
     * @return
     */
    @PostMapping("/user/api/p/code")
    public Result registerCode(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        dmUserService.sendVerifyCode(phone);
        return  ResponseApi.setSuccess();
    }

    /**
     * 验证手机号是否被使用
     * @param map 手机号/邮箱
     * @return
     */
    @PostMapping("/user/api/p/checkEmail")
    public Result checkEmail(@RequestBody Map<String, String> map){
        dmUserService.checkEmail(map.get("phone"));
        return ResponseApi.setSuccess();
    }

    /**
     * 用户注册
     * @param userRegisterDto 注册数据
     */
    @PostMapping("user/api/p/register")
    public Result  register(@RequestBody UserRegisterDto userRegisterDto){
        dmUserService.register(userRegisterDto);
        return ResponseApi.setSuccess(Constants.REGISTER_SUCCESS);
    }


    @PostMapping("/user/api/p/login")
    public Result login(@RequestBody DmUser dmUser){
        Object[] login = dmUserService.login(dmUser.getPhone(), dmUser.getPassword());
        return ResponseApi.setSuccess(login);
    }

    /**
     * 刷新token
     * @param token
     * @return
     */
    @PostMapping("/user/api/v/replaceToken")
    public Result replaceToken(String token){
        TokenVo tokenVo = dmUserService.replaceToken(token);
        return ResponseApi.setSuccess(tokenVo);
    }

    /**
     * 展示个人详情
     * @param token
     * @return
     */
    @PostMapping("/user/api/v/queryUserInfoByToken")
    public Result queryUserInfoByToken(@RequestHeader String token){
        UserInfoVo userInfoVo = dmUserService.queryUserInfoByToken(token);
        return ResponseApi.setSuccess(userInfoVo);
    }

}
